package com.lovo.project.servlet;

import com.lovo.project.enums.UserRoleCodeEnum;
import com.lovo.project.exception.OurException;
import com.lovo.project.model.RoleDO;
import com.lovo.project.service.UserRoleService;
import com.lovo.project.service.impl.UserRoleServiceImpl;
import com.lovo.project.vo.ResultVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "ShowRoleServlet",value = "/showRole")
public class ShowRoleServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int userId = Integer.parseInt(request.getParameter("userId"));
        UserRoleService userRoleService = new UserRoleServiceImpl();
        Map<String, Object> map = new HashMap<>();
        List<RoleDO> roleList = userRoleService.findAllRole();
        List<Integer> roleIdList = userRoleService.findRoleByUserId(userId);
        map.put("all",roleList);
        map.put("have",roleIdList);
        response.getWriter().println(ResultVO.success().add(map).toJson());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
